import React from 'react';
import './storeManage.less';
import {browserHistory} from 'react-router';
import $ from 'jquery';
import { Button, Table, Pagination, message,Modal} from 'antd';
import config from '../../../config/config';
const allRouter=config.router;
const token = sessionStorage.getItem('token');
class StoreManage extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            storeList:[],//门店列表
            pagesize: 12,// 每页显示的页数
            pageindex: 1,// 当前页
            storesCount:0,//门店总数
        }
    }
    componentWillMount() {
        //强制下线逻辑
        // let url=allRouter.patientOrigin+'/cmmuseum/list';
        // //console.log(url);
        // $.ajax({
        //     url:url,
        //     type: "GET",
        //     async: true,
        //     headers: { Authorization: 'Bearer '+sessionStorage.getItem('token')},
        //     success:data=>{
        //         console.log(data);
        //         if(data.code===200){
        //             //给列表每一项增加索引
        //             let dedicateStores=[];
        //             if (data.cmmuseums.length > 0) {
        //                 data.cmmuseums.forEach((ele, i) => {
        //                     if(ele.state==="禁用"){
        //                         dedicateStores.push(ele._id);
        //                     }
        //                 });
        //                 console.log('dfagsdgf');
        //                 console.log(dedicateStores);
        //                 if(JSON.parse(sessionStorage.getItem('UserInfo')).shop){
        //                     if(JSON.parse(sessionStorage.getItem('UserInfo')).shop.length>0){
        //                         if(dedicateStores.indexOf(JSON.parse(sessionStorage.getItem('UserInfo')).shop[0])!==-1){
        //                             if(JSON.parse(sessionStorage.getItem('UserInfo')).roleName!=="超级管理员"){
        //                                 message.info('门店被禁用，您已被强制下线!!',3);
        //                                 browserHistory.push('login');
        //                             }
        //                         }
        //                     }
        //                 }
        //             }
        //         }else{
        //             console.log("获取门店列表出错");
        //         }
        //     },
        //     error:err=>{
        //         console.log(err);
        //     }
        // });
    }
    componentDidMount() {
        //获取门店列表
        this.getStoreList();
    }
    //获取门店列表
    getStoreList() {
        let url=allRouter.patientOrigin+'/cmmuseum/list';
        //console.log(url);
        $.ajax({
            url:url,
            type: "GET",
            async: true,
            headers: { Authorization: 'Bearer '+sessionStorage.getItem('token')},
            success:data=>{
                console.log(data);
                if(data.code===200){
                    //给列表每一项增加索引
                    if (data.cmmuseums.length > 0) {
                        data.cmmuseums.forEach((ele, i) => {
                            ele.key= i + 1;
                            ele.showAddress=ele.address.replace(/,/g,'')+ele.addressdetail
                        });
                    }
                    console.log(data);
                    this.setState({
                        storeList:data.cmmuseums
                    })
                }else{
                    console.log("获取门店列表出错");
                }
            },
            error:err=>{
                console.log(err);
            }
        });
    }
    //分页
    changePage(pageindex) {
        this.setState({
            pageindex
        }, () => {
            // this.getStoreList();
        });
    }

    //编辑门店信息
    editStore(record) {
        browserHistory.push('addStoreForm/'+record._id);
        console.log(record._id);
    }
    //编辑门店状态
    editStoreState(recode){
        let that=this;
        //实际更改状态
       let actualEditState=()=>{
            let url=allRouter.patientOrigin+'/cmmuseum/update';
            $.ajax({
                url:url,
                type: "POST",
                async: true,
                headers: { Authorization: 'Bearer '+sessionStorage.getItem('token')},
                data:{
                    id:recode._id,
                    state:currentState,
                },
                success:data=>{
                    console.log(data);
                    if(data.code===200){
                        that.getStoreList();
                    }else{
                        message.info('更改状态错误');
                        // console.log("更改状态错误");
                    }
                },
                error:err=>{
                    console.log(err);
                }
            });
        };
        let currentState=recode.state;
        if(currentState==="启用"){
            currentState="禁用";
            Modal.confirm({
                title: '禁用门店',
                className:'confirm-error',
                content: '禁用门店之后除管理员之外其它角色不能进入后台，是否禁用?',
                onCancel: () => { },
                onOk: () => {
                    actualEditState();
                }
            })
        }else if(currentState==="禁用"){
            currentState="启用";
            actualEditState();
        }


    }

    jumpToPageIndex(){
        // this.setState({
        //     pageindex
        // })
    }
    render() {
        let index = this.state.pageindex;
        let size = this.state.pagesize;
        const data = this.state.storeList.slice(size*(index-1),size*index);
        //return text.length <=0 ? "单店" : text.length > 0 ? "多店" : ""
        const columns = [
            {
                title: '序号',
                dataIndex: 'key',
                width: 50,
            },
            {
                title: '门店名称',
                dataIndex: 'name',
                width: 150,
            }, {
                title: '管理员',
                dataIndex: 'managername',
                width: 80,
            }, {
                title: '关联门店',
                dataIndex: 'managershops',
                width: 70,
                 render: text => {return <span>{text.length==0?'单店':'多店'}</span>}
            }, {
                title: '门店属性',
                dataIndex: 'type',
                width: 150
            }, {
                title: '联系方式',
                dataIndex: 'managerphone',
                width: 120
            }, {
                title: '地址',
                dataIndex: 'showAddress',
                width: 250
            }, {
                title: '门店座机',
                dataIndex: 'phone',
                width: 120
            }, {
                title: '状态',
               dataIndex: 'state',
                width: 50,
               render: (text,record) => <Button onClick={this.editStoreState.bind(this,record)} type={text=='启用'?'primary':text=='禁用'?'danger':''}>{text=='启用'?'已启用，点击可禁用':text=='禁用'?'已禁用，点击可启用':''}</Button>
            }, {
                title: '操作',
                width: 150,
                render: (text, record) => (
                    <div>
                        <Button onClick={this.editStore.bind(this, record)} type="primary" >编辑</Button>
                    </div>
                )
            }
        ];
        return (
            <div className="container">
                <div className="store-manage-content">
                    {/*增加按钮*/}
                    <div className="storeAdd">
                        <Button onClick={() => { browserHistory.push('addStoreForm/0') }}  type="primary"> <img src={require('../../../../images/add.png')} alt=""/>新增门店</Button>
                    </div>
                    {/* 门店列表 */}
                    <div className="all-roles-table">
                        <Table
                            rowKey="_id"
                            columns={columns}
                            dataSource={data}
                            pagination={false}
                            bordered
                        />
                    </div>
                    {/*分页*/}
                    <div className="pagination">
                        <Pagination showQuickJumper current={this.state.pageindex} total={this.state.storeList.length || 1} defaultPageSize={this.state.pagesize} onChange={this.changePage.bind(this)} />
                    </div>
                </div>
            </div>
        )
    }
}
export default StoreManage;